package com.taxi.driver.module.main.mine.setting.volume;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;

import com.taxi.driver.common.Application;
import com.taxi.driver.data.user.UserRepository;

import javax.inject.Inject;

/**
 * 监听系统音量的广播
 */
public class SystemVolumeRceiver extends BroadcastReceiver {

    @Inject
    UserRepository mUserRepository;

    public SystemVolumeRceiver() {
        Application.getAppComponent().inject(this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
            int currVolume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
            mUserRepository.setSystemVolumeSize(100 * currVolume / maxVolume);
        }
    }
}
